package com.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mail.common.DataResult;
import com.mail.dto.EmailTemplatesDto;
import com.mail.mapper.EmailTemplatesMapper;
import com.mail.pojo.EmailTemplates;
import com.mail.pojo.User;
import com.mail.service.EmailTemplatesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class EmailTemplatesServiceImpl extends ServiceImpl<EmailTemplatesMapper, EmailTemplates> implements EmailTemplatesService {

    @Resource
    private EmailTemplatesMapper emailTemplatesMapper;


    @Override
    @Transactional
    public DataResult addEmailTemplates(EmailTemplatesDto emailTemplatesDto) {
        if(emailTemplatesDto.getUserId()==null || emailTemplatesDto.getPersonOrTeam()==null){
            return DataResult.error("参数格式错误！");
        }
        long userId = emailTemplatesDto.getUserId();
        int personOrTeam = emailTemplatesDto.getPersonOrTeam();
        String emailTitle=emailTemplatesDto.getEmailTitle();
        String emailContent=emailTemplatesDto.getEmailContent();
        if(!StringUtils.isNotBlank(emailTitle) || !StringUtils.isNotBlank(emailContent)){
            return DataResult.error("参数格式错误！");
        }
        if(personOrTeam==1){
            long teamId = emailTemplatesDto.getTeamId();
            EmailTemplates emailTemplates=new EmailTemplates(userId,emailTitle,emailContent,personOrTeam,teamId);
            emailTemplatesMapper.insert(emailTemplates);
        }else{
            EmailTemplates emailTemplates=new EmailTemplates(userId,emailTitle,emailContent,personOrTeam);
            emailTemplatesMapper.insert(emailTemplates);
        }
        return DataResult.success("添加成功！");
    }

    @Override
    @Transactional
    public DataResult updateEmailTemplates(Map<String, Object> map) {
        long id = Long.parseLong((String) map.get("id"));
        String userId1=(String) map.get("id");
        String personOrTeam=(String) map.get("personOrTeam");
        String emailTitle=(String) map.get("emailTitle");
        String emailContent=(String) map.get("emailContent");
        String teamId1=(String) map.get("teamId");

        UpdateWrapper<EmailTemplates> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        if(StringUtils.isNotBlank(userId1)){
            long userId = Long.parseLong((String) map.get("userId"));
            wrapper.set("user_id", userId);
        }
        if(StringUtils.isNotBlank(personOrTeam)){
            int personOrTeam1 = Integer.parseInt((String) map.get("personOrTeam"));
            wrapper.set("person_or_team", personOrTeam1);
        }
        if(StringUtils.isNotBlank(teamId1)){
            long teamId = Long.parseLong((String) map.get("teamId"));
            wrapper.set(!(teamId ==0), "team_id", teamId);
        }
        wrapper.set(StringUtils.isNotBlank(emailTitle), "email_title", emailTitle);
        wrapper.set(StringUtils.isNotBlank(emailContent), "email_content", emailContent);
        emailTemplatesMapper.update(null, wrapper);
        return DataResult.success("修改成功！");
    }

    @Override
    @Transactional
    public DataResult deleteEmailTemplates(long id) {
        emailTemplatesMapper.deleteById(id);
        return DataResult.success("删除成功！");
    }

    @Override
    public DataResult queryEmailTemplatesPage(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        String thing=(String) map.get("thing");
        Page<EmailTemplates> pageInfo = new Page<>(page, limit);
        QueryWrapper<EmailTemplates> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(thing)) {
            wrapper.like("email_title", thing).or().like("email_content", thing);
        }
        return DataResult.success(emailTemplatesMapper.selectPage(pageInfo,wrapper));
    }

    @Override
    public DataResult queryEmailTemplatesPersonal(Map<String,Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        long id = Long.parseLong((String) map.get("id"));
        String personOrTeam1=(String) map.get("personOrTeam");
        Page<EmailTemplates> pageInfo = new Page<>(page, limit);
        QueryWrapper<EmailTemplates>wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",id);
        if(StringUtils.isNotBlank(personOrTeam1)){
            int personOrTeam = Integer.parseInt((String) map.get("personOrTeam"));
            wrapper.eq("person_or_team",personOrTeam);
        }
        return DataResult.success(emailTemplatesMapper.selectPage(pageInfo,wrapper));
    }
}
